﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieCollectionManager.DataModel;

namespace MovieCollectionManager
{
    public partial class LoansForm : Form
    {
        public LoansForm()
        {
            InitializeComponent();
        }

        private void LoansForm_Load(object sender, EventArgs e)
        {
            BindMovies();
        }

        private void BindMovies()
        {
            using (var db = new MovieCollectionConteiner())
            {
                var moviesResult = (from movie in db.Movies
                                    where movie.Lent == true
                                    select movie);

                if (moviesResult.Count() == 0)
                    return;

                listBoxLentMovies.DataSource = moviesResult.ToList();
                listBoxLentMovies.DisplayMember = "Title";
                listBoxLentMovies.ValueMember = "Id";
            }
            textBoxMoviesLentCount.Text = listBoxLentMovies.Items.Count.ToString();
        }

        private void listBoxLentMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (var db = new MovieCollectionConteiner())
            {
                int selectedMovieId = ((Movie)listBoxLentMovies.SelectedItem).Id;
                Loan loanResult = (from loan in db.Loans
                                   where loan.MovieId == selectedMovieId
                                   select loan).First();

                textBoxLentTo.Text = loanResult.Receiver;
                textBoxDate.Text = loanResult.LoanDate.ToString();
                textBoxReturnDate.Text = loanResult.ReturnDate.ToString();
                textBoxPhone.Text = loanResult.ReceiverPhone.ToString();
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
